﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NCT.Framework;
using VISS.API.Users;
using VISS.Web.Frontend;

namespace VISS.Web.Frontend.Modules.User.Profile {
    public partial class ProfilePage : System.Web.UI.Page {
        protected string UserNameUrl = "";
        protected UserContext userContext = new UserContext();
        protected UserEnt userEnt = new UserEnt();
        protected string userKey;
        protected void Page_Load(object sender, EventArgs e) {
            userKey = ConvertUtils.ToString(Request.QueryString["key"]);
            userContext = LoginFactory.GetCurrentUser();
            if (userContext == null) {
                string url = AppSettings.RootURL + "profile?u=" + userKey;
                Response.Redirect("/login?url=" + url);
                return;
            }
            if (userKey == "") {
                Response.Redirect("/error?t=" + (int)EErrorType.ProfileNotFound);
                return;
            }

            userEnt = StructureMapFactory.GetInstance<IUser>().GetByUserKey(userKey);

          
            if (userEnt == null || ((userEnt.Status & EUserStatus.Hidden) > 0) && userContext.UserKey!=userEnt.UserKey ) {
                Response.Redirect("/error?t=" + (int)EErrorType.ProfileNotFound);
                return;
            }

            IUserGeneralProfile _objectGeneralProfile = StructureMapFactory.GetInstance<IUserGeneralProfile>();

            UserGeneralProfileEnt userGeneralProfileEnt = _objectGeneralProfile.GetByUserKey(userKey);
            UserApperanceProfileEnt userApperanceProfileEnt = StructureMapFactory.GetInstance<IUserAppearanceProfile>().GetByUserKey(userKey);
            UserLivingProfileEnt userLivingProfileEnt = StructureMapFactory.GetInstance<IUserLivingProfile>().GetByUserKey(userKey);
            UserFamilyProfileEnt userFamilyProfileEnt = StructureMapFactory.GetInstance<IUserFamilyProfile>().GetByUserKey(userKey);
            UserCultureProfileEnt userCultureProfileEnt = StructureMapFactory.GetInstance<IUserCultureProfile>().GetByUserKey(userKey);

            UserGeneralConditionEnt userGeneralConditionEnt = StructureMapFactory.GetInstance<IUserGeneralCondition>().GetByUserKey(userKey);

            UserContactInfoEnt userContactEnt = StructureMapFactory.GetInstance<IUserContactInfo>().GetByUserKey(userKey);

            //Set Avatar
            idAvatarBox.AvatarPath = LoginFactory.RefeshAvatarPath(userKey, userGeneralProfileEnt.Gender); //userContext.AvatarPath;

            if (userGeneralProfileEnt != null) {
                //Set General Account Box
                idGeneralAccountBox.FullName = userGeneralProfileEnt.Name;
                idGeneralAccountBox.Gender = userGeneralProfileEnt.Gender;
                idGeneralAccountBox.BirthDate = userGeneralProfileEnt.BirthDate;
                idGeneralAccountBox.Age = userGeneralProfileEnt.Age;
                idGeneralAccountBox.CityID = userGeneralProfileEnt.CityID;
                idGeneralAccountBox.City = userGeneralProfileEnt.City;
                idGeneralAccountBox.Trust = userGeneralProfileEnt.Trusted;
                idGeneralAccountBox.LastLogin = userEnt.LastLogin;
                idGeneralAccountBox.Interest = StructureMapFactory.GetInstance<IUserFollower>().CountByFollowedUserKey(userKey);
                UserNameUrl = userGeneralProfileEnt.Name;
                idGeneralAccountBox.CountPoint = "100";
                //Set Blast
                if (userGeneralProfileEnt.Blast == "")
                    idBlast.ContentBlast = "Chào mừng bạn đến với ViSS";
                else
                    idBlast.ContentBlast = userGeneralProfileEnt.Blast;

                //Set Personal Box Info
                idPersonal.Job = userGeneralProfileEnt.Job;
                idPersonal.JobID = userGeneralProfileEnt.JobID;
                idPersonal.Education = userGeneralProfileEnt.Education;
                idPersonal.EducationID = userGeneralProfileEnt.EducationID;
                idPersonal.Hobby = userGeneralProfileEnt.Hobby;
                if (userFamilyProfileEnt != null) {
                    idPersonal.Marital = userFamilyProfileEnt.MaritalStatus;
                    idPersonal.MaritalID = userFamilyProfileEnt.MaritalStatusID;
                }
                if (userCultureProfileEnt != null) {
                    idPersonal.Religion = userCultureProfileEnt.Religion;
                    idPersonal.ReligionID = userCultureProfileEnt.ReligionID;
                }
                if (userLivingProfileEnt != null) {
                    idPersonal.Smoking = userLivingProfileEnt.Smoking;
                    idPersonal.SmokingID = userLivingProfileEnt.SmokingID;
                }

                idPersonal.LookingFor = userGeneralProfileEnt.LookingFor;

                //Set ActionBox info
                idActionBox.UserKey = userEnt.UserKey;
                idActionBox.ImageCount = userGeneralProfileEnt.ImageCount;

                //Set Right
                if ((userContext.Status & EUserStatus.Verified) == EUserStatus.Verified) {
                    idActionBox.CanDoAction = true;
                    idGeneralAccountBox.CanEditFullName = false;
                }
                else {
                    idActionBox.CanDoAction = false;
                    idGeneralAccountBox.CanEditFullName = true;
                }
            }

            //Set Personal Info
            if (userApperanceProfileEnt != null) {
                idPersonal.Height = userApperanceProfileEnt.Height;

                idPersonal.Weight = userApperanceProfileEnt.Weight;
            }
            if (userLivingProfileEnt != null)
                idPersonal.Income = userLivingProfileEnt.Income;

            //Set General Condition
            idGeneralCondition.GeneralCondition = userGeneralConditionEnt;

            //Set ContactInfo
            if (userContactEnt != null)
            {
                idContact.GeneralContact = userContactEnt;
            }
            //Set Edit Permission For UserControl
            if (userContext.UserKey == userEnt.UserKey) {
                idGeneralAccountBox.IsEditGeneral = true;
                idBlast.IsEditBlast = true;
                idPersonal.IsEditPersional = true;
                idAvatarBox.IsChangeAvatar = true;
                idActionBox.IsPersonal = true;
                idSameProfile.Display = false;
                idGeneralCondition.IsEditPersional = true;
                idInterestedProfile.InterestedProfiles = StructureMapFactory.GetInstance<IUserFollower>().GetByFollowerUserKey(userKey);
                idInterestedProfile.Display = true;
            }
            else {
                idGeneralAccountBox.IsEditGeneral = false;
                idBlast.IsEditBlast = false;
                idPersonal.IsEditPersional = false;
                idAvatarBox.IsChangeAvatar = false;
                idActionBox.IsPersonal = false;
                idGeneralCondition.IsEditPersional = false;

                int totalRecord = 0;
                UserSrhEnt srhEnt = new UserSrhEnt();
                srhEnt.CityID = idSameProfile.CityID = userGeneralProfileEnt.CityID;
                srhEnt.Gender = idSameProfile.Gender = userGeneralProfileEnt.Gender;
                if (userGeneralProfileEnt.Age - 5 >= 18) {
                    srhEnt.ToBirthDate = new DateTime(userGeneralProfileEnt.BirthDate.Year + 5, 12, 31);
                    idSameProfile.FromAge = (byte)(userGeneralProfileEnt.Age - 5);
                }
                else {
                    idSameProfile.FromAge = 18;
                    srhEnt.ToBirthDate = new DateTime(DateTime.Now.Year - 18, 12, 31);
                }
                if (userGeneralProfileEnt.Age + 5 <= 90) {
                    srhEnt.FromBirthDate = new DateTime(userGeneralProfileEnt.BirthDate.Year - 5, 1, 1);
                    idSameProfile.ToAge = (byte)(userGeneralProfileEnt.Age + 5);
                }
                else {
                    idSameProfile.ToAge = 90;
                    srhEnt.FromBirthDate = new DateTime(DateTime.Now.Year - 90, 1, 1);
                }
                idSameProfile.Profiles = _objectGeneralProfile.QuickSearch(srhEnt, EUserQuickSearchOrderType.Random, 1, 3, out totalRecord);
                idSameProfile.ExceptUserKey = userGeneralProfileEnt.UserKey;
                idSameProfile.Display = true;
            }

            // SEO
            IHeadSeo seo = this.Master as IHeadSeo;
            seo.Title = StringResources.SEO.TitleUserProfile + " | " + userGeneralProfileEnt.Name + " | " + StringResources.SEO.TitleDefault;
            seo.Description = StringResources.SEO.DescriptionDefault;
            seo.Keywords = StringResources.SEO.KeywordsDefault;
        }
    }
}
